'''
003 SELF CONTROL
Controls:       Touch
Description:    Don't touch the button
'''
import pygame
import spyral
import random  


class game003(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        
        self.player = player;
        self.button = button();
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.pig = Image.Image(Dic.images['003pig'], (660, 259))
        self.talk = Image.Image(Dic.images['003talk'], (280, 65))
        
        self.pushed = False;
        self.won = True;
                
        self.group.add(self.button, self.pig, self.talk, self.timer);
        
        
    def on_enter(self):
        bg = spyral.util.load_image(Dic.images['003bg'])
        self.camera.set_background(bg)
        

    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(game003(self.player))
            if (self.pushed == False):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.button.rect.collidepoint(pygame.mouse.get_pos()) == True):
                        self.button.setPushed();
                        self.changeImages();
                        self.won = False;
                        self.pushed = True;
        
        
        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
    
    
    def winCondition(self):
        return self.won;
    
    def changeImages(self):
        self.pig.image = spyral.util.load_image(Dic.images['003devil'])
        self.talk.image = spyral.util.load_image(Dic.images['003gotcha'])
        bg = spyral.util.new_surface(960, 640)
        bg.fill(Dic.colors['black'])
        self.camera.set_background(bg);
        



class button(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.button = spyral.util.load_image(Dic.images['003button']);
        self.buttonpressed = spyral.util.load_image(Dic.images['003buttonpressed']);
        self.image = self.button;
        self.rect.center = spyral.director.get_camera().get_rect().center;
        self.layer = 2;
        self.pushed = False;
        self.counter = 0;
    
    def update(self, tick):
        if (self.pushed == True):
            self.counter += 1;
            if (self.counter == 5):
                self.image = self.button;
                self.pushed = False;
    
    def setPushed(self):
        self.image = self.buttonpressed;
        self.pushed = True;

        



if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game003(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;